package com.bitoon.basketdudes.javamail;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.BodyPart;
import javax.mail.Message.RecipientType;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import org.apache.log4j.Logger;

public class Mail {

    public int sendMail(String para, String asunto, String texto) {
        return sendMail(para,asunto,texto,false);
    }
    
    public int sendMail(String para, String asunto, String texto, boolean esHtml) {
        int retorno = 0;
        Properties p = new Properties();
        p.setProperty("mail.smtp.host", "localhost");
        p.setProperty("mail.smtp.port", "25");
        p.setProperty("mail.smtp.user", "user");
        p.setProperty("mail.smtp.auth", "false");

        Session mailSession = Session.getDefaultInstance(p);
        mailSession.setDebug(true);

        try {
            MimeMessage mensaje = new MimeMessage(mailSession);
            InternetAddress ia = new InternetAddress("info@bitoon.com", "BasketDudes");
            mensaje.setFrom(ia);
            mensaje.addRecipient(RecipientType.TO, new InternetAddress(para));
            mensaje.setSubject(asunto);

            if (esHtml) {
                MimeMultipart multipart = new MimeMultipart("alternative");
                BodyPart body = new MimeBodyPart();
                body.setHeader("Content-Type","text/html; charset=\"utf-8\"");
                body.setHeader("Content-Transfer-Encoding", "quoted-printable");
                body.setContent(texto, "text/html; charset=\"utf-8\"");
                multipart.addBodyPart(body);
                mensaje.setContent(multipart);
            } else {
                mensaje.setText(texto);
            }

            Transport t = mailSession.getTransport("smtp");
            t.connect();
            t.send(mensaje);
            t.close();
        } catch (Exception e) {
            
        }

        return retorno;
    }

    public int sendMailPlantilla(Logger log, String plantilla, String para, String asunto, String cuerpo) {
        return sendMailPlantilla(log,plantilla,para,asunto,cuerpo,"");
    }

    public int sendMailPlantilla(Logger log, String plantilla, String para, String asunto, String cuerpo, String footer) {

        int retorno = 0;

        try {
        
            // Leer la plantilla
            //ServletActionContext.getServletContext().getRealPath("/bd/xml/manager_semana.xml")
            File fPlantilla = new File(plantilla);
            FileInputStream fisp = new FileInputStream(fPlantilla);
            Scanner scanner = new Scanner(fisp);
            StringBuilder contenidoPlantilla = new StringBuilder();

            while (scanner.hasNextLine()) {
                contenidoPlantilla.append(scanner.nextLine());
            }

            String contenido = contenidoPlantilla.toString();
            contenido = contenido.replace("#TEXTO#", cuerpo);
            contenido = contenido.replace("#FOOTER#", footer);

            fisp.close();

            sendMail(para, asunto, contenido, true);
        } catch (Exception e) {
            log.error("Error: ", e);
        }

        return retorno;
    }
}
